<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

// 引入模型
use App\Models\Business\Business as BusinessModel;
use App\Models\Product\Product as ProductModel;
use App\Models\Product\Type as ProductTypeModel;
use App\Models\Product\Cart as CartModel;
// 订单商品模型
use App\Models\Product\OrderProduct as OrderProductModel;
// 引入Validator 
use Illuminate\Support\Facades\Validator;


class Product extends Controller
{
    // 分类
    public function type()
    {
        $data = ProductTypeModel::orderBy('weigh', 'desc')->get();

        return success('', $data);
    }

    // 首页
    public function index()
    {
        // 推荐
        $recommendlist = ProductModel::where(['flag' => '3', 'status' => '1',])->whereIn('rentstatus', ['1', '3'])->orderBy('createtime', 'desc')->limit(5)->get();

        $hotlist = ProductModel::where(['flag' => '2', 'status' => '1',])->whereIn('rentstatus', ['1', '3'])->orderBy('createtime', 'desc')->limit(5)->get();

        $newlist = ProductModel::where(['flag' => '1', 'status' => '1',])->whereIn('rentstatus', ['1', '3'])->orderBy('createtime', 'desc')->limit(5)->get();

        $data = [
            'recommendlist' => $recommendlist,
            'hotlist' => $hotlist,
            'newlist' => $newlist
        ];

        return success('', $data);
    }
    // 列表
    public function list()
    {
        $flag = request('flag', '');
        $order = request('order', 'createtime');
        $keyword = request('keyword', '');
        $typeid = request('typeid', 0);

        $where = [];
        $where['status'] = '1';

        // 标签
        if (!empty($flag)) {
            $where['flag'] = $flag;
        }
        // 关键字搜索
        if (!empty($keyword)) {
            $where[] = ['name', 'like', "%$keyword%"];
        }

        // 分类
        if ($typeid > 0) {
            $where['typeid'] = $typeid;
        }
        $data = ProductModel::where($where)->whereIn('rentstatus', ['1', '3'])->orderBy($order, 'desc')->get();
       
        return success('', $data);
    }
    // 根据商品分类查询
    public function typeShopList()
    {
        $typeid = request('typeid', 0);
        $where = [];
        $where['status'] = '1';
        if ($typeid != 0) {
            $where['typeid'] = $typeid;
        }
        $typeShopList = ProductModel::where($where)->whereIn('rentstatus', ['1', '3'])->orderBy('createtime', 'desc')->limit(5)->get();
        return success('', $typeShopList);
    }
    // 详情
    public function info()
    {
        $proid = request('proid', 0);
        $busid = request('busid', 0);

        $info = ProductModel::find($proid);
        // 判断用户是否登陆
        if ($busid > 0) {
            $count = CartModel::where(['busid' => $busid])->count();
            $info['count'] = $count;
        }
        $where=['proid' => $proid];
        $where[]=['rate','!=',null];
        $orderProInfo=OrderProductModel::with('business')->where($where)->get();
        $commentData=[];
        if($orderProInfo){
            $orderProInfo=$orderProInfo->toArray();
           
            foreach($orderProInfo as $item){
                $prodata=[
                    'id'=>$item['id'],
                    'rate'=>$item['rate'],
                    'comment'=>$item['comment'],
                    'busName'=>$item['business']['nickname'],
                    'busAvatar'=>$item['business']['avatar_text'],
                    'proimg'=>$item['proimg_list'],
                    'updatetime'=>$item['updatetime']
                ];
                $commentData[]=$prodata;
            }
        }
        $data=['info' => $info,'orderProInfo' => $commentData];
        return success('', $data);
    }
    // 人工智能聊天
    public function qingyunke()
    {
        $msg = request('msg', '');
        $url = "http://api.qingyunke.com/api.php";
        $data = [
            'key' => 'free',
            'msg' => $msg
        ];
        $paramsStr = http_build_query($data);
        $response=httpRequest($url,$paramsStr);
        $restrut=json_decode($response,true);
        $restrut['content']=preg_replace('/{br}/','<br/>',$restrut['content']);
        if($restrut['result']==0){
            return success('', $restrut['content']);
        }
        return error($restrut);
    }
}
